package com.doucat.support.fast;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.doucat.support.component.fragment.base.ToolbarFragment;
import com.doucat.support.component.tab.VpAdapter;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.tonly.doucatlibrary.R;

import java.util.ArrayList;
import java.util.List;

/**
 * -----------------------------------------------------------------
 * Copyright (C) 2021-2031, by tonly, All rights reserved.
 * -----------------------------------------------------------------
 * File: TabIndicatorFragment.java
 * Author: wangfy
 * Create: 2021/7/21 10:38
 */
public class TabIndicatorFragment extends ToolbarFragment {
    private List<Fragment> fragments;
    private TabLayout tabLayout;
    private TabLayoutMediator mediator;
    private ViewPager2 viewPager2;
    private VpAdapter vpAdapter;
    private List<String> tabTexts;
    @Override
    public void onRootViewCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onRootViewCreate(inflater,container,savedInstanceState);
        setTitleFrameLayout(R.layout.fragment_tab_indicator);
        tabTexts=new ArrayList<>();
        fragments=new ArrayList<>();
        tabLayout=findViewById(R.id.tabLayout);
        viewPager2 = findViewById(R.id.vp2);
        vpAdapter=new VpAdapter(getActivity(),fragments);
        viewPager2.setAdapter(vpAdapter);
        viewPager2.registerOnPageChangeCallback(onPageChangeCallback);
        mediator=new TabLayoutMediator(tabLayout, viewPager2,true,false, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(TabLayout.Tab tab, int position) {
                View tabView = LayoutInflater.from(getContext()).inflate(R.layout.item_tablayout_indicator,null);
                TextView tvTabText = tabView.findViewById(R.id.tvTabText);
                tvTabText.setText(tabTexts.get(position));
                tab.setCustomView(tabView);
            }
        });
        //要执行这一句才是真正将两者绑定起来
        mediator.attach();

        runOnThread(new Runnable() {
            @Override
            public void run() {
                loadingTabLayoutFragment();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        vpAdapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }

    public void loadingTabLayoutFragment(){

    }

    public void addTab(String title, Fragment fragment){
        tabTexts.add(title);
        fragments.add(fragment);
    }

    private ViewPager2.OnPageChangeCallback onPageChangeCallback=new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            for (int i = 0; i < tabLayout.getTabCount(); i++) {
                TabLayout.Tab tab = tabLayout.getTabAt(i);
                View tabView = tab.getCustomView();
                TextView tvTabText = tabView.findViewById(R.id.tvTabText);
                if (tab.getPosition() == position) {
                    tvTabText.setSelected(true);
                } else {
                    tvTabText.setSelected(false);
                }
            }
        }
    };
    @Override
    public void onDestroy() {
        super.onDestroy();
        mediator.detach();
        viewPager2.unregisterOnPageChangeCallback(onPageChangeCallback);
    }
}